﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using IndustrialSmokeCalc.Resources;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.IO.IsolatedStorage;

using System.Device.Location;
using Windows.Devices.Geolocation;
using Microsoft.Phone.Maps.Services;

namespace IndustrialSmokeCalc
{
    public partial class MainPage : PhoneApplicationPage
    {
        List<int> grayscaleValue = new List<int>();
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (DataSingleton.Instance.Picture != null)
            {
                WriteableBitmap wb = new WriteableBitmap(DataSingleton.Instance.Picture);

                //ConvertToGrayscale(PictureSingleton.Instance.Picture);
                int value = (int)grayscaleValue.Average();
                int result = GetRingelmannValue(value);
                string formatResult = String.Format("Ori = {0}%, RSC = {1}%", GetPercentage(value), GetPercentage(result));
                txtPixel.Text = formatResult;

                img1.Source = RotateBitmap(wb, wb.PixelHeight, wb.PixelWidth);
            }
        }

        private void ConvertToGrayscale(BitmapImage bmpImageSource)
        {
            //Uri uri = new Uri(path, UriKind.Relative);
            //StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
            //BitmapImage bmp = new BitmapImage();
            //bmp.SetSource(resourceInfo.Stream);
            //WriteableBitmap bitmapImage = new WriteableBitmap(bmp);

            WriteableBitmap bitmapImage = new WriteableBitmap(bmpImageSource);
            for (var y = 0; y < bitmapImage.PixelHeight; y++)
            {
                for (var x = 0; x < bitmapImage.PixelWidth; x++)
                {
                    var pixelLocation = bitmapImage.PixelWidth * y + x;
                    var pixel = bitmapImage.Pixels[pixelLocation];
                    var pixelbytes = BitConverter.GetBytes(pixel);
                    var bwPixel = (byte)(.299 * pixelbytes[2] + .587 * pixelbytes[1] + .114 * pixelbytes[0]);
                    pixelbytes[0] = bwPixel;
                    pixelbytes[1] = bwPixel;
                    pixelbytes[2] = bwPixel;
                    bitmapImage.Pixels[pixelLocation] = BitConverter.ToInt32(pixelbytes, 0);
                    grayscaleValue.Add(bwPixel);
                }
            }

            img2.Source = RotateBitmap(bitmapImage, bitmapImage.PixelHeight, bitmapImage.PixelWidth);
        }

        private WriteableBitmap RotateBitmap(WriteableBitmap source, int width, int height)
        {
            var target = new WriteableBitmap(width, height); 
            int sourceIndex = 0;
            int targetIndex = 0;
            for (int x = 0; x < source.PixelWidth; x++)
            {
                for (int y = 0; y < source.PixelHeight; y++)
                {
                    sourceIndex = x + y * source.PixelWidth;
                    targetIndex = (source.PixelHeight - y - 1) + x * target.PixelWidth;
                    target.Pixels[targetIndex] = source.Pixels[sourceIndex];
                }
            }
            return target;
        }

        private int GetRingelmannValue(int value)
        {
            int result = 0;
            int[] RingelmannValue = new int[]{255, 204, 153, 102, 51, 0};
            int[] diff = new int[6];
            for (int i = 0; i < 6; i++)
            {
                diff[i] = RingelmannValue[i] - value;
                if (diff[i] < 0) diff[i] *= -1;
            }

            for (int i = 0; i < 6; i++)
            {
                if (diff[i] == diff.Min())
                {
                    result = RingelmannValue[i];
                }
            }

            return result;
        }

        private int GetPercentage(int value)
        {
            return (int)(100 - (value * 100 / 255));
        }

        public void SaveResult(string value)
        {
            if (settings["result"] == null) settings.Add("result", value);
            else settings["result"] = value;
        }

        String loc = "";

        public String Loci
        {
            get { return loc; }
            set 
            { 
                loc = value;
                MessageBox.Show(loc);
            }
        }

        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            //NavigationService.Navigate(new Uri("/Camera.xaml", UriKind.Relative));
            Location l = new Location();
            //l.GetAddress();            
            //MessageBox.Show(l.Loc);
            //l.GetLocation();
            //GetCurrentCoordinate();
        }
    }
}